package com.example.webpractice.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.webpractice.dao.KechengfenleiDao;
import com.example.webpractice.dto.KechengfenleiDTO;
import com.example.webpractice.po.KechengfenleiPO;
import com.example.webpractice.vo.KechengfenleiVO;
import com.example.webpractice.vo.PageVo;
import com.example.webpractice.vo.Po2Vo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service("kechengfenleiService")
public class KechengfenleiService {

    @Autowired
    private KechengfenleiDao dao;

    public PageVo queryPage(int pages, int limit, QueryWrapper queryWrapper) {
        Page<KechengfenleiPO> pagePO = dao.selectPage(new Page<>(pages, limit), queryWrapper);
        return Po2Vo.toVoPage(pagePO, KechengfenleiVO.class);
    }

    public KechengfenleiVO selectKechengfenlei(String name) {
        KechengfenleiPO po = dao.selectOne(new QueryWrapper<KechengfenleiPO>().eq("kechengfenlei", name));
        if (po != null) {
            KechengfenleiVO vo = new KechengfenleiVO();
            BeanUtils.copyProperties(po, vo);
            return vo;
        }
        return null;
    }

    public void create(KechengfenleiDTO kechengfenleiDTO) {
        KechengfenleiPO po = new KechengfenleiPO();
        BeanUtils.copyProperties(kechengfenleiDTO, po);
        dao.insert(po);
    }

    public int update(KechengfenleiDTO kechengfenleiDto) {
        KechengfenleiPO po = new KechengfenleiPO();
        BeanUtils.copyProperties(kechengfenleiDto, po);
        return dao.updateById(po);
    }

    public KechengfenleiVO selectById(Long id) {
        KechengfenleiPO po = dao.selectOne(new QueryWrapper<KechengfenleiPO>().eq("id", id));
        KechengfenleiVO vo = new KechengfenleiVO();
        BeanUtils.copyProperties(po, vo);
        return vo;
    }

    public int deleteBatchIds(List list) {
        return dao.deleteByIds(list);
    }


    public List<String> getOption() {
        QueryWrapper qe = new QueryWrapper<KechengfenleiPO>().select("kechengfenlei");
        List<KechengfenleiPO> list = dao.selectList(qe);
        return list.stream().map(KechengfenleiPO::getKechengfenlei).collect(Collectors.toList());
    }
}
